package com.yanqu.road.server.manger.activity.worldcup;

import com.yanqu.road.dao.impl.activity.climbtower.ClimbTowerConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.climbtower.*;
import com.yanqu.road.entity.activity.climbtower.enums.eClimbTowerCellType;
import com.yanqu.road.entity.activity.climbtower.enums.eClimbTowerEventType;
import com.yanqu.road.entity.activity.worldcup.WorldCupConfig;
import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.player.ClimbTowerActivityBussiness;
import com.yanqu.road.logic.bussiness.player.WorldCupActivityBussiness;
import com.yanqu.road.pb.activity.ClimbTowerProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.climbtower.ClimbTowerModule;
import com.yanqu.road.server.gameplayer.module.activity.worldcup.WorldCupModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class WorldCupMgr extends TempMgr {
    static WorldCupConfig config;

    public static ActivityInfo getActivityInfo() {
        if (config == null) {
            return null;
        }
        return config.getActivityInfo();
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload WorldCupMgr Activity start");
        ActivityInfo tempActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.WorldCup.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no WorldCupMgr Activity in show time");
            config = null;
        } else {
            tempActivityInfo = openActivityInfoList.get(0);
            int tmpActivityId = tempActivityInfo.getActivityId();
            WorldCupConfig configTemp = new WorldCupConfig();
            configTemp.setActivityInfo(tempActivityInfo);

            //初始化配置
            //config配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);

            configTemp.setBallerConfig(WorldCupActivityBussiness.getWorldCupPlayerConfigList(tmpActivityId));
            configTemp.setSkillConfig(WorldCupActivityBussiness.getWorldCupSkillConfigList(tmpActivityId));

            configTemp.setScoreItemId(activityConfigMap.get("EXCHANGE_SCORE_ITEM_ID").getIntValue());
            configTemp.setBallUniformItemId(activityConfigMap.get("WORLDCUP_COST_ITEM_ID").getIntValue());
            configTemp.setTenRecruitCostNum(activityConfigMap.get("WORLDCUP_TEN_RECRUIT_COST_NUM").getIntValue());
            configTemp.setOneRecruitCostNum(activityConfigMap.get("WORLDCUP_ONE_RECRUIT_COST_NUM").getIntValue());
            configTemp.setTenRecruitNeedValue(activityConfigMap.get("WORLDCUP_TEN_RECRUIT_NEED_VALUE").getIntValue());
            List<Integer> weightAttrs = StringUtils.stringToIntegerList(activityConfigMap.get("WORLDCUP_WEIGHT_PARAM").getValue(), ",");
            if (weightAttrs.size() == 4) {
                configTemp.setSkillWeightParam_1(weightAttrs.get(0));
                configTemp.setSkillWeightParam_2(weightAttrs.get(1));
                configTemp.setSkillWeightParam_3(weightAttrs.get(2));
                configTemp.setSkillWeightParam_4(weightAttrs.get(3));
            }

            List<NormalWeightItem> scoreRangeList = new ArrayList<>();
            String scoreAddStr = activityConfigMap.get("WORLDCUP_SCORE_ADD").getValue();
            String[] ranges = scoreAddStr.split("\\|");
            for (String range : ranges) {
                List<Integer> weightInfo = StringUtils.stringToIntegerList(range, ",");
                NormalWeightItem scoreRange = new NormalWeightItem();
                scoreRange.setWeight(weightInfo.get(1));
                scoreRange.setValue(BigInteger.valueOf(weightInfo.get(0)));//千分比
                scoreRangeList.add(scoreRange);
            }
            configTemp.setScoreRandomConfig(scoreRangeList);

            config = configTemp;
        }
        getLogger().info("reload WorldCupMgr Activity finish");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            WorldCupModule module = gamePlayer.getModule(WorldCupModule.class);
            module.syncConfig();
            module.initUserData();
            module.syncUserData();
        }
    }

    public static int getActivityId() {
        if (getActivityInfo() == null) {
            return 0;
        }
        return getActivityInfo().getActivityId();
    }

    public static WorldCupConfig getConfig() {
        return config;
    }
}
